// pages/prouser/prouser.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    users: [
      {
        user_id: 1,
        user_name: "胡强"
      },
      {
        user_id: 2,
        user_name: "申鹏杰"
      },
      {
        user_id: 3,
        user_name: "肖园园1"
      },
      {
        user_id: 4,
        user_name: "肖园园2"
      },
      {
        user_id: 5,
        user_name: "肖园园3"
      },
      {
        user_id: 6,
        user_name: "肖园园4"
      },
      {
        user_id: 7,
        user_name: "肖园园5"
      },
      {
        user_id: 8,
        user_name: "肖园园6"
      },
      {
        user_id: 9,
        user_name: "肖园园7"
      },
      {
        user_id: 10,
        user_name: "肖园园8"
      }
    ],
    ids: [],
    position: 'right',
    checkedUser: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      ids: app.globalData.ids,
      checkedUser: app.globalData.checkedUser
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  handUserChange: function (e) {
    // console.log(e);
    const value = e.detail.value;
    const index = this.data.checkedUser.indexOf(value);
    let pages = getCurrentPages();
    let prevPage = pages[pages.length-2];
    // console.log(index);
    if(index === -1){
      this.data.checkedUser.push(value);
      for (var i = 0; i < this.data.users.length; i++) {
        if (value == this.data.users[i].user_name) {
          this.data.ids.push(this.data.users[i].user_id);
        }
      }
    }else{
      this.data.checkedUser.splice(index, 1);
      this.data.ids.splice(index, 1);
    }
    this.setData({
      checkedUser: this.data.checkedUser
    });
    app.globalData.checkedUser = this.data.checkedUser;
    app.globalData.ids = this.data.ids;
  }
  
})